package day13.thread02;

/**
 * @author Xiao
 * @date 2021/8/5
 */
public class Ticket2 implements Runnable {
    private static int ticketCount = 100;
    private static Object o = new Object();
    @Override
    public void run() {
        while (true) {
            //synchronized()可以传入任意对象，但多个线程必须使用通一把锁
            synchronized (o) {

                if (ticketCount <= 0) {
                    break;
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() +
                        "卖了1张票，还剩下" + --ticketCount + "张");
            }
        }
    }
}
